package com.xinghai.quartz.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinghai.common.constant.CacheConstants;
import com.xinghai.common.core.redis.RedisCache;
import com.xinghai.common.utils.DateUtils;
import com.xinghai.system.domain.*;
import com.xinghai.system.domain.vo.QdStoreVo;
import com.xinghai.system.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.xinghai.common.utils.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 定时任务调度测试
 *
 * @author xinghai
 */
@Component("ryTask")
public class RyTask
{

    @Autowired
    private IQdStoreService storeService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private IQdUserService qdUserService;

    @Autowired
    private IQdUserWalletService userWalletService;

    @Autowired
    private IQdOrderService orderService;

    @Autowired
    private IQdPayService payService;
    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

    public void updateStoreInfo()
    {
        List<QdStore> list = storeService.list(new QdStore());
        list.forEach(m -> {
            QdStoreVo dto = new QdStoreVo();
            BeanUtils.copyProperties(m, dto);
            dto.setReservationStartDateDay(DateUtils.getDate()+" "+m.getReservationStartDate());
            dto.setReservationEndDateDay(DateUtils.getDate()+" "+m.getReservationEndDate());
            dto.setSaleStartDateDay(DateUtils.getDate()+" "+m.getSaleStartDate());
            dto.setSaleEndDateDay(DateUtils.getDate()+" "+m.getSaleEndDate());
            redisCache.setCacheObject(CacheConstants.STORE_INFO + m.getId(), dto);
        });


    }
    public void updateUserWallet(){
        QdUser user1 = new QdUser();
        List<QdUser> list2 = qdUserService.list(user1);
        //所有人的钱包今日订单清空
        String string = list2.stream().map(m -> String.valueOf(m.getId())).collect(Collectors.joining(","));
        QdUserWallet w = new QdUserWallet();
        w.setUserIds(string);
        List<QdUserWallet> list3 = userWalletService.list(w);
        list3.forEach(m->{
            m.setNowMoney(BigDecimal.ZERO);
            m.setNowOrderNum(0);
        });
        userWalletService.updateBatchById(list3);
    }

    /**
     * 订单释放
     */
    public void updateDJOrder(){
        QueryWrapper<QdOrder> order = new QueryWrapper<>();
        order.apply("FIND_IN_SET(status,'0,2')");
        order.eq("seller_status","0");
        List<QdOrder> list = orderService.list(order);
        list.forEach(m->{
            m.setStatus("0");
            m.setDealTime(new Date());
        });
        orderService.updateBatchById(list);
    }


    /**
     * 订单释放
     */
    public void autoBuyOrder(){
        orderService.autoBuyOrder();
        //TODO 总金额预约
        orderService.autoAmountOrder();
    }
}
